package com.ls.huawei;

import java.util.*;

class Solution {
    public int jump(int[] nums) {
        int maxPostion = 0;
        int end = 0;
        int step = 0;

        for(int i = 0; i < nums.length - 1; i++){
            maxPostion = Math.max(maxPostion, i + nums[i]);

            if(i == end){
                end = maxPostion;
                step++;
            }
        }

        return step;
    }

    public String reverseParentheses(String s) {

        char[] chs = s.toCharArray();
        Stack<Character> stack = new Stack<Character>();
        List<Character> list = new ArrayList<>(s.length());
        for(int i = 0; i < chs.length; i++){
            if(chs[i] != ')'){
                stack.push(chs[i]);
            }else{
                while(true){
                    char t = stack.pop();
                    if(t == '('){
                        break;
                    }
                    list.add(t);
                }
                for (char s1 : list) {
                    stack.push(s1);
                }
                list.clear();
            }
        }

        StringBuffer  sb = new StringBuffer ();
        while(!stack.isEmpty()){
            char t = stack.pop();
            sb.insert(0, t);
        }

        return sb.toString();
    }

    public int[] dailyTemperatures(int[] temperatures) {
        int[] ans = new int[temperatures.length];

        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < temperatures.length; i++) {

            if(stack.isEmpty()){
                stack.push(i);
            }else{

                while(!stack.isEmpty()){

                    Integer top = stack.peek();
                    if(temperatures[i] > temperatures[top]){
                        stack.pop();
                        ans[top] = i - top;
                    }else if(temperatures[i] < temperatures[top]){
                        stack.push(i);
                    }else{
                        break;
                    }
                }

                if(stack.isEmpty()){
                    stack.push(i);
                }

            }
        }

        return ans;
    }

    public int[] dailyTemperatures2(int[] temperatures) {
        int[] ans = new int[temperatures.length];

        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < temperatures.length; i++) {
            int t = temperatures[i];

            while(!stack.isEmpty() && t > temperatures[stack.peek()]){
                Integer preIndex = stack.pop();
                ans[preIndex] = i - preIndex;
            }
            stack.push(i);
        }

        return ans;
    }

    public int lengthOfLongestSubstring(String s) {
        char[] chs = s.toCharArray();

        Set<Character> set = new HashSet<>();
        int l = 0, max = 0;

        for(int i = 0; i < chs.length && l < chs.length; i++){

            char ch = chs[i];

            if(!set.contains(ch)){
                set.add(ch);

            }else{
                set.remove(chs[l]);
                l++;
            }

            max = Math.max(max, i - l);
        }

        return max;
    }

    public static void main(String[] args) {
        Solution s = new Solution();
//        System.out.println(s.reverseParentheses("(abcd)"));
//        System.out.println(s.reverseParentheses("(u(love)i)"));

//        int[] temp = new int[]{73, 74, 75, 71, 69, 72, 76, 73};
//        int[] ans = s.dailyTemperatures2(temp);
//        for (int i : ans) {
//            System.out.print(i + " ");
//        }
//        System.out.println();


        System.out.println(s.lengthOfLongestSubstring("abcabcbb"));
    }
}